<?php
    if ( ! defined('BASEPATH'))
        exit('No direct script access allowed');
    class Reservation extends CCMIS_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
        public function index()
        {
            $this->setup_view('pfmis/pfmis/reservation/view');
        }
        public function view_all() //home page of pfmis
        {
            $this->setup_view('pfmis/pfmis/reservation/view_all');
        }
        public function addreservation()
        {
            if($this->input->server('REQUEST_METHOD') == 'GET')
                $this->setup_view('pfmis/pfmis/reservation/add');
            else
            {
                $this->load->database();
                $getMaxResIdQ = 'SELECT MAX(ReservationID) as MaxRes FROM pf_reservation';
                $getMaxResIdR = $this->db->query($getMaxResIdQ);
                $getMaxResId = $getMaxResIdR->result();
                
                $arrayItemName = explode('-',$_POST['item']);
                $getItemIdQ = 'SELECT FacilityID FROM pf_facilities WHERE FacilityName = \''.$arrayItemName[0].'\'';
                $getItemIdR = $this->db->query($getItemIdQ);
                $getItemId = $getItemIdR->result();
                
                $arrayTime = explode(':',$_POST['time']);
                
                if($_POST['ampm'] == "PM")
                {
                    $arrayTime[0] = $arrayTime[0] + 12;
                }
                $hourTime = $arrayTime[0];
                $minTime = $arrayTime[1];
                
                if($_POST['hours'] == 1)
                {
                    $hourTime = $hourTime + 1;
                }
                else if($_POST['hours'] == 1.5)
                {
                    $hourTime = $hourTime + 1;
                    if($minTime == 30)
                    {
                        $hourTime = $hourTime + 1;
                        $minTime = 0;
                    }
                }
                else if($_POST['hours'] == 2)
                {
                    $hourTime = $hourTime + 2;
                }
                else if($_POST['hours'] == 3)
                {
                    $hourTime = $hourTime + 3;
                }
                else if($_POST['hours'] == 5)
                {
                    $hourTime = $hourTime + 5;
                }
                
                $addReservationQ = 'INSERT INTO pf_reservation VALUES(\''.(($getMaxResId[0]->MaxRes) + 1).'\',\''.$getItemId[0]->FacilityID.'\',\''.$_POST['prof'].'\',\''.$_POST['reservedby'].'\',\''.$arrayTime[0].':'.$arrayTime[1].'\',\''.$hourTime.':'.$minTime.'\',\''.date('Y').'-'.$_POST['month'].'-'.$_POST['date'].'\')';
                $this->db->query($addReservationQ);
                $this->setup_view('pfmis/pfmis/reservation/view');
            }
        }
    }
?>